home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume89 / aplictns / anncs.1 next >
Lisp/Scheme  |  1989-07-13  |  28KB  |  1,064 lines

  1. Path: xanth!ames!apple!sun-barr!newstop!sun!swap!page
  2. From: page%swap@Sun.COM (Bob Page)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v89i168:  anncs - amiga neural net construction set
  5. Message-ID: <115218@sun.Eng.Sun.COM>
  6. Date: 13 Jul 89 02:44:07 GMT
  7. Sender: news@sun.Eng.Sun.COM
  8. Lines: 1053
  9. Approved: page@sun.com
  10.  
  11. Submitted-by: jal@pan.cs.wayne.edu (Jason Leigh)
  12. Posting-number: Volume 89, Issue 168
  13. Archive-name: applications/anncs.1
  14.  
  15. The ANNCS is a set of functions that allows quick and easy construction of
  16. neural networks.  The system is very simple and as a result much can be
  17. enhanced.  Eventually I may attach a graphical interface that allows you to
  18. visually create neural networks and convert the images to ANNCS Lisp
  19. function calls.  This is by no means a competitor for the Rochester
  20. Connectionist Simulator but hay, what the heck, it's at least running on my
  21. favorite computer!
  22.  
  23. # This is a shell archive.
  24. # Remove anything above and including the cut line.
  25. # Then run the rest of the file through 'sh'.
  26. # Unpacked files will be owned by you and have default permissions.
  27. #----cut here-----cut here-----cut here-----cut here----#
  28. #!/bin/sh
  29. # shar: SHell ARchive
  30. # Run the following text through 'sh' to create:
  31. #    and.lsp
  32. #    neural.lsp
  33. #    neural.readme
  34. #    xor.lsp
  35. #    xor2.lsp
  36. # This is archive 1 of a 1-part kit.
  37. # This archive created: Wed Jul 12 19:38:16 1989
  38. echo "extracting and.lsp"
  39. sed 's/^X//' << \SHAR_EOF > and.lsp
  40. X;;; @@FILE and.lsp
  41. X;;; Copyright (C) 1989 By Jason Leigh 5/5/89
  42. X
  43. X;;; Redistribute this program at will.  If you decide to use this program
  44. X;;; as part of some commercial package you are developing, by all means
  45. X;;; proceed, all I ask is that you give me a small amount of credit for it.
  46. X
  47. X;;; Creating an AND network using the functions in neural.lsp
  48. X
  49. X;;; The activation function is Relay and a diagram of this
  50. X;;; network can be found in:
  51. X;;; Bernard Widrow, IEEE Computer, March 1988, p.33
  52. X
  53. X(ClearNetwork)
  54. X
  55. X;;; Select the Relay function as the output function.
  56. X(defun OutputFunction (sum threshold)
  57. X    (Relay sum threshold))
  58. X
  59. X(CreateInputLine 'i1)            ;Labelled as i1
  60. X(CreateInputLine 'i2)            ;Labelled as i2
  61. X(CreateNeuron 'n1 0)            ;Labelled as n1
  62. X                    ;This is the output neuron.
  63. X(CreateInputLine 'i3)            ;Labelled as i3
  64. X
  65. X(SetInputLine 'i3 1)            ;Input i3 is permanently set to 1.
  66. X(ConnectInputToNeuron 'i1 'n1 1)    ;Connect input 1 to neuron 1
  67. X(ConnectInputToNeuron 'i2 'n1 1)    ;Connect input 2 to neuron 1
  68. X(ConnectInputToNeuron 'i3 'n1 -1.5)    ;Connect input 3 to neuron 1
  69. X
  70. X
  71. X;;; Now show the four outcomes of a two input AND function.
  72. X
  73. X(printf "Input: 1 1\n")
  74. X(SetInputLine 'i1 1)        ; Set inputs 1 and 2 to 1
  75. X(SetInputLine 'i2 1)
  76. X(UpdateNetwork 10 '(or1))    ; Update the system.
  77. X(printf "Output: " (NeuronOutput 'n1) "\n")
  78. X
  79. X(printf "Input: -1 1\n")
  80. X(SetInputLine 'i1 -1)        ; Set inputs 1 and 2 to 1,-1
  81. X(SetInputLine 'i2 1)
  82. X(UpdateNetwork 10 '(or1))    ; Update the system.
  83. X(printf "Output: " (NeuronOutput 'n1) "\n")
  84. X
  85. X(printf "Input: -1 -1\n")
  86. X(SetInputLine 'i1 -1)        ; Set inputs 1 and 2 to -1,-1
  87. X(SetInputLine 'i2 -1)
  88. X(UpdateNetwork 10 '(or1))    ; Update the system.
  89. X(printf "Output: " (NeuronOutput 'n1) "\n")
  90. X
  91. X(printf "Input: 1 -1\n")
  92. X(SetInputLine 'i1 1)        ; Set inputs 1 and 2 to 1,-1
  93. X(SetInputLine 'i2 -1)
  94. X(UpdateNetwork 10 '(or1))    ; Update the system.
  95. X(printf "Output: " (NeuronOutput 'n1) "\n")
  96. SHAR_EOF
  97. echo "extracting neural.lsp"
  98. sed 's/^X//' << \SHAR_EOF > neural.lsp
  99. X;;; @@FILE neural.lsp
  100. X;;; Neural Network Construction Set (Vr 1.0 : 5/5/89)
  101. X;;; Copyright (C) 1989 by Jason Leigh  5/5/89
  102. X
  103. X;;; Comments, questions and donations may be sent to:
  104. X;;; Jason Leigh
  105. X;;; 538 Mackenzie Hall, Wayne State University, Detroit, MI 48202.
  106. X;;; email: jal@zeus.cs.wayne.edu
  107. X
  108. X;;; Redistribute this program at will.  If you decide to use this program
  109. X;;; as part of some commercial package you are developing, by all means
  110. X;;; proceed, all I ask is that you give me a small amount of credit for it.
  111. X
  112. X;;; This was written as a practice session in using XLISP objects.
  113. X;;; Neurons are ideally suited to object oriented programming since
  114. X;;; each neuron must contain its own current set of attributes.
  115. X;;; See the files: and.lsp, xor.lsp and xor2.lsp for implementations of
  116. X;;; AND and XOR functions using neural networks.  It is the best
  117. X;;; way to learn how to use the PUBLIC functions below.
  118. X
  119. X;;; Fool-proof code is at a minimum so if you don't use these
  120. X;;; functions just the way they are supposed to be used, you'll
  121. X;;; get a lot of error messages by XLISP.
  122. X
  123. X;;; @@SECTION printf
  124. X;;;
  125. X;;; @@REQ
  126. X;;; parameters
  127. X;;;    Any LISP s-expression to be printed.  Allows multiple expressions.
  128. X;;; e.g. (printf "hello there" 1 2 3 "wow \n")
  129. X;;; @@END-REQ
  130. X;;;
  131. X;;; @@FUNC
  132. X;;;    Works like printf in C or print in BASIC allowing multiple
  133. X;;; expressions to be printed on one line.  Far more convenient than
  134. X;;; prin1, princ etc...
  135. X;;; @@END-FUNC
  136. X;;;
  137. X;;; @@STATUS
  138. X;;; Complete.
  139. X;;; @@END-STATUS
  140. X
  141. X;;; PUBLIC
  142. X(defun printf (&rest parameters)
  143. X    (dolist (item parameters)
  144. X        (princ item)))
  145. X
  146. X;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  147. X
  148. X;;; Network is an a-list of neuron number and neuron object.
  149. X;;; PRIVATE
  150. X(setq *network* ())
  151. X
  152. X;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  153. X;;;             CLASS DEFINITIONS FOR NEURONS
  154. X
  155. X;;; Class of a neuron contains the instance variables:
  156. X;;; threshold, sum and list of input connections.
  157. X;;; threshold is the threshold value associated with each neuron
  158. X;;; to determine whether the neuron should fire or not.  This is
  159. X;;; usually determined empirically.
  160. X;;; sum is the scalar product of all the inputs and connection strengths
  161. X;;; entering a neuron.
  162. X;;; listOfInConnections is an a-list of connections that go into a
  163. X;;; particular neuron and the connection strength associated with the
  164. X;;; connection.
  165. X
  166. X;;; PRIVATE
  167. X(setq NeuronClass (send Class :new 
  168. X        '(threshold sum listOfInConnections)))
  169. X
  170. X;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  171. X;;;                METHODS
  172. X
  173. X;;; @@SECTION NeuronClass:output
  174. X;;;
  175. X;;; @@REQ
  176. X;;; None
  177. X;;; @@END-REQ
  178. X;;;
  179. X;;; @@FUNC
  180. X;;; This method
  181. X;;; calls the appropriate function to compute the output of a
  182. X;;; neuron.  Uses the sum and threshold instance variables
  183. X;;; of the given object.
  184. X;;; @@END-FUNC
  185. X;;;
  186. X;;; @@STATUS
  187. X;;; Complete.
  188. X;;; There are several possible
  189. X;;; output functions that may be used.  These are listed as:
  190. X;;; Relay, Linear and Sigmoid.
  191. X;;; @@END-STATUS
  192. X
  193. X(send NeuronClass :answer :output () 
  194. X    '((if (null listOfInConnections) sum
  195. X                     (OutputFunction sum threshold))))
  196. X
  197. X;;; @@SECTION Relay
  198. X;;;
  199. X;;; @@REQ
  200. X;;; sum
  201. X;;;    scalar product of all inputs and weights.
  202. X;;; threshold
  203. X;;;    threshold of neuron.
  204. X;;; @@END-REQ
  205. X;;;
  206. X;;; @@FUNC
  207. X;;;    Output function of a neuron.  The relay function
  208. X;;; sends a 1 if the sum >= the threshold and -1 otherwise.
  209. X;;; This conforms to the ADALINE.
  210. X;;; @@END-FUNC
  211. X;;;
  212. X;;; @@STATUS
  213. X;;; Complete.
  214. X;;; @@END-STATUS
  215. X
  216. X(defun Relay (sum threshold)
  217. X    (if (>= sum threshold) 1 -1))
  218. X
  219. X;;; @@SECTION Linear
  220. X;;;
  221. X;;; @@REQ
  222. X;;; sum
  223. X;;;    scalar product of all inputs and weights.
  224. X;;; threshold
  225. X;;;    threshold of neuron.
  226. X;;; @@END-REQ
  227. X;;;
  228. X;;; @@FUNC
  229. X;;;    Output function of a neuron.  The linear function
  230. X;;; does not use the threshold but rather sends the sum directly
  231. X;;; as output; I believe this is used in the Neocognitron model.
  232. X;;; @@END-FUNC
  233. X;;;
  234. X;;; @@STATUS
  235. X;;; Complete.
  236. X;;; @@END-STATUS
  237. X
  238. X(defun Linear (sum threshold)
  239. X    sum)
  240. X
  241. X;;; @@SECTION Sigmoid
  242. X;;;
  243. X;;; @@REQ
  244. X;;; sum
  245. X;;;    scalar product of all inputs and weights.
  246. X;;; threshold
  247. X;;;    threshold of neuron.
  248. X;;;
  249. X;;; @@END-REQ
  250. X;;;
  251. X;;; @@FUNC
  252. X;;;    Output function of a neuron.  The sigmoidal function
  253. X;;; goes toward 0 for large -ve numbers and 1 for large +ve numbers
  254. X;;; This is suitable for the Back-propagation model where a 
  255. X;;; semi-linear continuous output function is needed.
  256. X;;; @@END-FUNC
  257. X;;;
  258. X;;; @@STATUS
  259. X;;; Complete.
  260. X;;; @@END-STATUS
  261. X
  262. X(defun Sigmoid (sum threshold)
  263. X    (/ 1.0 (+ 1.0 (exp (- 0.0 (+ (float sum) threshold))))))
  264. X
  265. X;;; @@SECTION NeuronClass:clear
  266. X;;;
  267. X;;; @@REQ
  268. X;;; None.
  269. X;;; @@END-REQ
  270. X;;;
  271. X;;; @@FUNC
  272. X;;; This method sets the SUM instance variable of an object to zero.
  273. X;;; @@END-FUNC
  274. X;;;
  275. X;;; @@STATUS
  276. X;;; Complete.
  277. X;;; @@END-STATUS
  278. X
  279. X(send NeuronClass :answer :clear () '((setq sum 0)))
  280. X
  281. X;;; @@SECTION NeuronClass:setThreshold
  282. X;;;
  283. X;;; @@REQ
  284. X;;; value
  285. X;;;    value to set the threshold.
  286. X;;; @@END-REQ
  287. X;;;
  288. X;;; @@FUNC
  289. X;;;    This method changes the current threshold value of a
  290. X;;; neuron.
  291. X;;; @@END-FUNC
  292. X;;;
  293. X;;; @@STATUS
  294. X;;; Complete.
  295. X;;; @@END-STATUS
  296. X
  297. X(send NeuronClass :answer :setThreshold '(value) '((setq threshold value)))
  298. X
  299. X;;; @@SECTION NeuronClass:addConnection
  300. X;;;
  301. X;;; @@REQ
  302. X;;; neuronId
  303. X;;;    Unique identifier that specifies an existing neuron.
  304. X;;; weight
  305. X;;;    Connection strength.
  306. X;;; @@END-REQ
  307. X;;;
  308. X;;; @@FUNC
  309. X;;;    Adds the neuron's Id and the connection strength between the
  310. X;;; neuron and the current neuron to the current neuron's list
  311. X;;; of input connections.
  312. X;;; The list of in connections is an a-list of neuron number and
  313. X;;; weight.
  314. X;;; @@END-FUNC
  315. X;;;
  316. X;;; @@STATUS
  317. X;;; Complete.
  318. X;;; @@END-STATUS
  319. X
  320. X(send NeuronClass :answer :addConnection '(neuronId weight)
  321. X
  322. X    '((setq listOfInConnections
  323. X         (append listOfInConnections
  324. X             `((,neuronId ,weight))))))
  325. X
  326. X;;; @@SECTION NeuronClass:getConnection
  327. X;;;
  328. X;;; @@REQ
  329. X;;; srcNeuronId
  330. X;;;    neuron Id
  331. X;;; @@END-REQ
  332. X;;;
  333. X;;; @@FUNC
  334. X;;;    Retrieves the neuronId and weight from the
  335. X;;; list of connections associated with a neuron object.
  336. X;;; @@END-FUNC
  337. X;;;
  338. X;;; @@STATUS
  339. X;;;
  340. X;;; @@END-STATUS
  341. X
  342. X(send NeuronClass :answer :getConnection '(srcNeuronId)
  343. X    '((assoc srcNeuronId listOfInConnections)))
  344. X
  345. X;;; @@SECTION NeuronClass:removeConnection
  346. X;;;
  347. X;;; @@REQ
  348. X;;; neuronIdToRemove
  349. X;;;    The neuron Id to search for in the list of input connections
  350. X;;; associated with a neuron object.
  351. X;;; destNeuronId
  352. X;;;    The neuron object to remove it from.
  353. X;;; Returns T if connection to remove exists; () otherwise.
  354. X;;; @@END-REQ
  355. X;;;
  356. X;;; @@FUNC
  357. X;;;    Removes a connection between two neurons.
  358. X;;; This simply  requires going to the destination neuron and removing
  359. X;;; the entry in the a-list ListOfInConnections.
  360. X;;; @@END-FUNC
  361. X;;;
  362. X;;; @@STATUS
  363. X;;; Complete.
  364. X;;; @@END-STATUS
  365. X
  366. X(send NeuronClass :answer :removeConnection '(neuronIdToRemove destNeuronId)
  367. X    '((let* ((connection (send (GetNeuron destNeuronId)
  368. X                  :getConnection neuronIdToRemove))
  369. X            (remaining (Delete connection 
  370. X                   listOfInConnections :test 'equal)))
  371. X            (if (null connection)
  372. X           (progn
  373. X             (printf "No connection: " neuronIdToRemove "->" 
  374. X                         destNeuronId
  375. X                         " exists.\n")
  376. X             ())
  377. X               (progn 
  378. X             (setq listOfInConnections remaining)
  379. X             T)))))
  380. X
  381. X;;; @@SECTION NeuronClass:setSum
  382. X;;;
  383. X;;; @@REQ
  384. X;;; value
  385. X;;;    Value to set the sum.
  386. X;;; @@END-REQ
  387. X;;;
  388. X;;; @@FUNC
  389. X;;;    Set the SUM instance variable of a neuron object to a particular
  390. X;;; value.
  391. X;;; @@END-FUNC
  392. X;;;
  393. X;;; @@STATUS
  394. X;;; Complete.
  395. X;;; @@END-STATUS
  396. X
  397. X(send NeuronClass :answer :setSum '(value)
  398. X    '((setq sum value)))
  399. X
  400. X;;; @@SECTION NeuronClass:getSum
  401. X;;;
  402. X;;; @@REQ
  403. X;;; None
  404. X;;; @@END-REQ
  405. X;;;
  406. X;;; @@FUNC
  407. X;;;    Get the SUM instance variable of a neuron object.
  408. X;;; @@END-FUNC
  409. X;;;
  410. X;;; @@STATUS
  411. X;;; Complete.
  412. X;;; @@END-STATUS
  413. X
  414. X(send NeuronClass :answer :getSum ()
  415. X    '(sum))
  416. X
  417. X;;; @@SECTION NeuronClass:update
  418. X;;;
  419. X;;; @@REQ
  420. X;;; None.
  421. X;;; @@END-REQ
  422. X;;;
  423. X;;; @@FUNC
  424. X;;; Method to update the sum in a neuron based on connections
  425. X;;; from inputs of other neurons.
  426. X;;; @@END-FUNC
  427. X;;;
  428. X;;; @@STATUS
  429. X;;; Complete.
  430. X;;; @@END-STATUS
  431. X
  432. X(send NeuronClass :answer :update ()
  433. X    '(
  434. X      (if (not (null listOfInConnections))
  435. X      (progn 
  436. X        (setq sum 0)
  437. X        (dolist (connection listOfInConnections)
  438. X        (setq sum (+ sum
  439. X         (* (NeuronOutput (car connection))
  440. X            (cadr connection)))))))))
  441. X
  442. X
  443. X;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  444. X;;;        HIGH LEVEL INTERFACES TO METHODS
  445. X
  446. X;;; @@SECTION ConnectNeuron
  447. X;;;
  448. X;;; @@REQ
  449. X;;; srcNeuron
  450. X;;; destNeuron
  451. X;;; weight
  452. X;;; @@END-REQ
  453. X;;;
  454. X;;; @@FUNC
  455. X;;;    Form a connection between the srcNeuron and destNeuron
  456. X;;; that has a connection strength given by weight.
  457. X;;; @@END-FUNC
  458. X;;;
  459. X;;; @@STATUS
  460. X;;; Complete.
  461. X;;; @@END-STATUS
  462. X
  463. X;;; Connect a source neuron to a destination neuron using a weight.
  464. X;;; PUBLIC
  465. X(defun ConnectNeuron (srcNeuron destNeuron weight)
  466. X    (send (GetNeuron destNeuron) :addConnection srcNeuron weight))
  467. X
  468. X;;; @@SECTION CreateNeuron
  469. X;;;
  470. X;;; @@REQ
  471. X;;; neuronId
  472. X;;;    Unique Id to give to neuron.
  473. X;;; threshold
  474. X;;;    Threshold to set in the neuron.
  475. X;;; @@END-REQ
  476. X;;;
  477. X;;; @@FUNC
  478. X;;; Create a new neuron that is identified by neuronId.
  479. X;;; If you have a friend who you think has the intelligence of a
  480. X;;; single neuron, use his/her name!
  481. X;;;
  482. X;;; @@END-FUNC
  483. X;;;
  484. X;;; @@STATUS
  485. X;;; Complete.
  486. X;;; @@END-STATUS
  487. X
  488. X;;; PUBLIC
  489. X(defun CreateNeuron (neuronId threshold)
  490. X    (if (GetNeuron neuronId)
  491. X       (printf "Identifier: " neuronId " already exists.\n")
  492. X       (progn
  493. X        (setq *network* 
  494. X            (append *network* `((,neuronId ,(MakeNeuron)))))
  495. X        (send (GetNeuron neuronId) :setThreshold threshold)
  496. X        (send (GetNeuron neuronId) :clear)
  497. X        neuronId)))
  498. X
  499. X;;; @@SECTION MakeNeuron
  500. X;;;
  501. X;;; @@REQ
  502. X;;; None
  503. X;;; @@END-REQ
  504. X;;;
  505. X;;; @@FUNC
  506. X;;; Invoke the :new method to create a new neuron object of class:
  507. X;;; NeuronClass.
  508. X;;; @@END-FUNC
  509. X;;;
  510. X;;; @@STATUS
  511. X;;; Complete.
  512. X;;; @@END-STATUS
  513. X
  514. X;;; Make a neuron by creating an object from the neuron class.
  515. X;;; PRIVATE
  516. X(defun MakeNeuron ()
  517. X    (send NeuronClass :new))
  518. X
  519. X;;; @@SECTION GetNeuron
  520. X;;;
  521. X;;; @@REQ
  522. X;;; neuronId
  523. X;;;    Id of neuron
  524. X;;; @@END-REQ
  525. X;;;
  526. X;;; @@FUNC
  527. X;;;    Get a neuron from the network.
  528. X;;; @@END-FUNC
  529. X;;;
  530. X;;; @@STATUS
  531. X;;;    Complete.
  532. X;;; @@END-STATUS
  533. X
  534. X;;; PUBLIC
  535. X(defun GetNeuron (neuronId)
  536. X    (cadr (assoc neuronId *network*)))
  537. X
  538. X;;; @@SECTION Examine
  539. X;;;
  540. X;;; @@REQ
  541. X;;; neuronId
  542. X;;;    Neuron Id
  543. X;;; @@END-REQ
  544. X;;;
  545. X;;; @@FUNC
  546. X;;;    Examine the inner parameters of a neuron.
  547. X;;; @@END-FUNC
  548. X;;;
  549. X;;; @@STATUS
  550. X;;; Complete.
  551. X;;; @@END-STATUS
  552. X
  553. X;;; Examine the inner instance variables of a neuron.
  554. X;;; PUBLIC
  555. X(defun Examine (neuronId)
  556. X    (send (GetNeuron neuronId) :show)
  557. X    (printf "Output: " (NeuronOutput neuronId) "\n"))
  558. X
  559. X;;; @@SECTION CreateInputLine
  560. X;;;
  561. X;;; @@REQ
  562. X;;; neuronId
  563. X;;;    Give a unique name to inputs into the network.
  564. X;;; @@END-REQ
  565. X;;;
  566. X;;; @@FUNC
  567. X;;;    Actually an input line is made a neuron with no threshold
  568. X;;; so that whatever goes into the neuron comes straight back out.
  569. X;;; This saves having to declare an input class.
  570. X;;; @@END-FUNC
  571. X;;;
  572. X;;; @@STATUS
  573. X;;; Complete.
  574. X;;; @@END-STATUS
  575. X
  576. X;;; PUBLIC
  577. X(defun CreateInputLine (neuronId)
  578. X    (CreateNeuron neuronId 0))
  579. X
  580. X;;; @@SECTION SetInputLine
  581. X;;;
  582. X;;; @@REQ
  583. X;;; neuronId
  584. X;;;    Input line Id.
  585. X;;; inputValue
  586. X;;;    Value to set input value to.
  587. X;;; @@END-REQ
  588. X;;;
  589. X;;; @@FUNC
  590. X;;; Set the output sum of a neuron to always equal some value.
  591. X;;; This disguises the fact that an input line is really a neuron
  592. X;;; with only a set output.
  593. X;;;
  594. X;;; @@END-FUNC
  595. X;;;
  596. X;;; @@STATUS
  597. X;;; Complete.
  598. X;;; @@END-STATUS
  599. X
  600. X;;; PUBLIC
  601. X(defun SetInputLine (neuronId inputValue)
  602. X    (send (GetNeuron neuronId) :setSum inputValue))
  603. X
  604. X;;; @@SECTION ConnectInputToNeuron
  605. X;;;
  606. X;;; @@REQ
  607. X;;; inputNum
  608. X;;;    Input line Id
  609. X;;; destNeuron
  610. X;;;    Destination neuron Id
  611. X;;; weight
  612. X;;;    Strength of connection
  613. X;;; @@END-REQ
  614. X;;;
  615. X;;; @@FUNC
  616. X;;; Connect an input line to a neuron.
  617. X;;; @@END-FUNC
  618. X;;;
  619. X;;; @@STATUS
  620. X;;; Complete.
  621. X;;; @@END-STATUS
  622. X
  623. X;;; PUBLIC
  624. X(defun ConnectInputToNeuron (inputNum destNeuron weight)
  625. X    (ConnectNeuron inputNum destNeuron weight))
  626. X
  627. X;;; @@SECTION UpdateNeuron
  628. X;;;
  629. X;;; @@REQ
  630. X;;; neuronId
  631. X;;;    neuron id.
  632. X;;; @@END-REQ
  633. X;;;
  634. X;;; @@FUNC
  635. X;;; Update a neuron by sending update message to neuron object.
  636. X;;; This recalculates the SUM.
  637. X;;; @@END-FUNC
  638. X;;;
  639. X;;; @@STATUS
  640. X;;; Complete.
  641. X;;; @@END-STATUS
  642. X
  643. X;;; PRIVATE
  644. X(defun UpdateNeuron (neuronId)
  645. X    (send (GetNeuron neuronId) :update))
  646. X
  647. X;;; @@SECTION UpdateNetwork
  648. X;;;
  649. X;;; @@REQ
  650. X;;; updateLimit
  651. X;;;    After how many updates through the network should
  652. X;;; results be reported.
  653. X;;; neuronList
  654. X;;;    List  of neuron Id's to examine when updateLimit is reached.
  655. X;;; @@END-REQ
  656. X;;;
  657. X;;; @@FUNC
  658. X;;; Update the system by updating every neuron.  This occurs
  659. X;;; repeatedly until the network outputs have stablized.
  660. X;;; Since some networks may take a long time to produce results,
  661. X;;; setting an updateLimit and neuronList will allow intermediate
  662. X;;; reporting of results while the network is updating.
  663. X;;; @@END-FUNC
  664. X;;;
  665. X;;; @@STATUS
  666. X;;; Complete.
  667. X;;; @@END-STATUS
  668. X
  669. X;;; PUBLIC
  670. X(defun UpdateNetwork (updateLimit neuronList)
  671. X    (let ((change ())
  672. X          (old    ())
  673. X          (updates 0))
  674. X        (loop
  675. X         (setq change ())
  676. X         (dolist (eachNeuronInNetwork *network*)
  677. X        (setq old (NeuronOutput (car eachNeuronInNetwork)))
  678. X        (UpdateNeuron (car eachNeuronInNetwork))
  679. X        (if (not (equal old 
  680. X               (NeuronOutput (car eachNeuronInNetwork))))
  681. X               (setq change T)))
  682. X         (setq updates (+ updates 1))
  683. X         (if (> updates updateLimit)
  684. X         (progn
  685. X            (setq updates 0)
  686. X             (ReportNeurons neuronList)))
  687. X         (if (null change) (return)))))
  688. X
  689. X;;; @@SECTION ReportNeurons
  690. X;;;
  691. X;;; @@REQ
  692. X;;; neuronList
  693. X;;;    List of neurons to examine.
  694. X;;; @@END-REQ
  695. X;;;
  696. X;;; @@FUNC
  697. X;;;    Examine a list of neurons.
  698. X;;; @@END-FUNC
  699. X;;;
  700. X;;; @@STATUS
  701. X;;; Complete.
  702. X;;; @@END-STATUS
  703. X
  704. X;;; PUBLIC
  705. X(defun ReportNeurons (neuronList)
  706. X    (cond ((null neuronList) T)
  707. X          (T (progn
  708. X           (printf "\n*****  Neuron Id: " (car neuronList) "\n")
  709. X           (Examine (car neuronList))
  710. X           (printf "\n")
  711. X           (ReportNeurons (cdr neuronList))))))
  712. X
  713. X;;; @@SECTION NeuronOutput
  714. X;;;
  715. X;;; @@REQ
  716. X;;; neuronId
  717. X;;;    Neuron Id
  718. X;;; @@END-REQ
  719. X;;;
  720. X;;; @@FUNC
  721. X;;;    Return the output of a neuron.
  722. X;;; @@END-FUNC
  723. X;;;
  724. X;;; @@STATUS
  725. X;;; Complete.
  726. X;;; @@END-STATUS
  727. X
  728. X;;; PUBLIC
  729. X(defun NeuronOutput (neuronId)
  730. X    (send (GetNeuron neuronId) :output))
  731. X
  732. X;;; @@SECTION ClearNetwork
  733. X;;;
  734. X;;; @@REQ
  735. X;;; None
  736. X;;; @@END-REQ
  737. X;;;
  738. X;;; @@FUNC
  739. X;;; Simply sets *network* to null thus losing all neurons defined so far.
  740. X;;; @@END-FUNC
  741. X;;;
  742. X;;; @@STATUS
  743. X;;; Complete.
  744. X;;; @@END-STATUS
  745. X
  746. X;;; Clear network of all neurons
  747. X;;; PUBLIC
  748. X(defun ClearNetwork ()
  749. X    (setq *network* ()))
  750. X
  751. X;;; @@SECTION ChangeWeight
  752. X;;;
  753. X;;; @@REQ
  754. X;;; srcNeuron
  755. X;;; destNeuron
  756. X;;; newWeight
  757. X;;; @@END-REQ
  758. X;;;
  759. X;;; @@FUNC
  760. X;;; Change the connection strength from srcNeuron to destNeuron to the
  761. X;;; new value given by newWeight.  This function was written so that
  762. X;;; feedback networks that learn by modifying their weights based
  763. X;;; on a comparison of target and actual results, can be designed.
  764. X;;; @@END-FUNC
  765. X;;;
  766. X;;; @@STATUS
  767. X;;; Complete.
  768. X;;; @@END-STATUS
  769. X
  770. X;;; PUBLIC
  771. X(defun ChangeWeight (srcNeuron destNeuron newWeight)
  772. X    (if (send (GetNeuron destNeuron)
  773. X          :removeConnection srcNeuron destNeuron)
  774. X        (ConnectNeuron srcNeuron destNeuron newWeight)))
  775. X
  776. X
  777. X;;; @@SECTION GetWeight
  778. X;;;
  779. X;;; @@REQ
  780. X;;; srcNeuron
  781. X;;; destNeuron
  782. X;;; @@END-REQ
  783. X;;;
  784. X;;; @@FUNC
  785. X;;; Find the connection strength between srcNeuron and destNeuron.
  786. X;;; @@END-FUNC
  787. X;;;
  788. X;;; @@STATUS
  789. X;;; Complete.
  790. X;;; @@END-STATUS
  791. X
  792. X;;; PUBLIC
  793. X(defun GetWeight (srcNeuron destNeuron)
  794. X    (let ((result (send
  795. X               (GetNeuron destNeuron)
  796. X               :getConnection srcNeuron)))
  797. X         (cadr result)))
  798. X
  799. X
  800. X;;; @@SECTION GetSum
  801. X;;;
  802. X;;; @@REQ
  803. X;;; neuronId
  804. X;;; @@END-REQ
  805. X;;;
  806. X;;; @@FUNC
  807. X;;; Find the SUM of a neuron. (i.e. Net)
  808. X;;; @@END-FUNC
  809. X;;;
  810. X;;; @@STATUS
  811. X;;; Complete.
  812. X;;; @@END-STATUS
  813. X
  814. X;;; PUBLIC
  815. X
  816. X(defun GetSum (neuronId)
  817. X    (send (GetNeuron neuronId) :getSum))
  818. SHAR_EOF
  819. echo "extracting neural.readme"
  820. sed 's/^X//' << \SHAR_EOF > neural.readme
  821. X    Amiga Neural Network Construction Set (Vr 1.0 : 5/5/59)
  822. X
  823. XThe following is a brief description of the files contained in
  824. Xa tiny XLISP Neural Network Construction Set (ANNCS):
  825. X
  826. XThe ANNCS is a set of functions that allows quick and easy construction
  827. Xof neural networks.  The system is very simple and as a result much
  828. Xcan be enhanced.  Eventually, if I have nothing better to do over the
  829. Xsummer, I will attach a graphical interface that allows you to
  830. Xvisually create neural networks and convert the images to ANNCS Lisp
  831. Xfunction calls.  This is by no means a competitor for the Rochester
  832. XConnectionist Simulator but hay, what the heck, it's at least running
  833. Xon my favorite computer!
  834. X
  835. XNeural.lsp    : Lisp source code for the construction set.
  836. XAnd.lsp        : Implementation of the AND function using neural networks
  837. X          constructed from functions in Neural.lsp
  838. X          Uses Widrow's ADALINE's
  839. XXor.lsp        : Implementation of the XOR function using neural networks
  840. X          constructed from functions in Neural.lsp
  841. X          Uses Widrow's ADALINE's
  842. XXor2.lsp    : Alternate implementation of the XOR function using
  843. X          neural networks constructed from functions in Neural.lsp
  844. X          Uses Rumelhart's network.
  845. X
  846. XSorry there is no demonstration of learning in the Backpropagation
  847. Xmodel but it can easily be constructed using the functions in this
  848. Xconstruction set; so why don't you do it and send me a copy!
  849. X
  850. XTo load the programs, fire up XLISP and type:
  851. X
  852. X(load "Neural.lsp")
  853. X(load "And.lsp")
  854. X(load "Xor.lsp")
  855. X(load "Xor2.lsp")
  856. X
  857. X*************************************************************************
  858. XFunctions that are present in version 1.0 of ANNCS:
  859. X
  860. XRead And.lsp, Xor.lsp and Xor2.lsp for a demonstration of how the
  861. Xfunctions may be used.
  862. X
  863. X(ClearNetwork)
  864. X    - Clear the network of all neurons.
  865. X
  866. X(CreateNeuron neuronId threshold)
  867. X    - Create a neuron and give it a name: neuronId 
  868. X      Set the threshold value to: threshold.
  869. X      e.g. (CreateNeuron 'myNeuron 2.3)
  870. X
  871. X(CreateInputLine inputId)
  872. X    - Create an input line and give it a name: inputId.
  873. X      e.g. (CreateInputLine 'input1)
  874. X
  875. X(SetInputLine inputId inputValue)
  876. X    - Set the input value to the input line: inputId to the
  877. X      value: inputValue.
  878. X      e.g. (SetInputLine 'input1 1)
  879. X
  880. X(ConnectInputToNeuron inputId destNeuron weight)
  881. X    - Connect an input line to a neuron.  The weight is the
  882. X      connection strength between the input line and neuron.
  883. X
  884. X(ConnectNeuron srcNeuron destNeuron weight)
  885. X    - Connect two neurons together.  I.e. Output of
  886. X      srcNeuron feeds into input of destNeuron with a connection
  887. X      strength of: weight.
  888. X
  889. X(GetNeuron neuronId)
  890. X    - Get a neuron with the given Id name.  This can be used to
  891. X      get an input line as well.
  892. X      e.g. (GetNeuron 'myNeuron)
  893. X      Returns the neuron object.
  894. X
  895. X(Examine neuronId)
  896. X    - Examine the contents of a neuron or input line.
  897. X      e.g. (Examine 'input1)
  898. X
  899. X(ReportNeurons neuronList)
  900. X    - Like examine except a list of neuron Id's are given.
  901. X      e.g. (ReportNeurons '(input1 'or1))
  902. X
  903. X(UpdateNetwork updateLimit neuronList)
  904. X    - Based on the current inputs and connections, update the
  905. X      entire network so that we can see what the output
  906. X      should be.  In some networks it may take a considerable
  907. X      number of iterations before the network finally stablizes
  908. X      to a result.  The parameter updateLimit is an integer
  909. X      that determines how many iterations the system is to
  910. X      update before the list of neuron Id's in neuronList are
  911. X      examined.  Updating then resumes.
  912. X
  913. X
  914. X(NeuronOutput neuronId)
  915. X    - Return the output of a particular neuron.
  916. X
  917. X(ChangeWeight srcNeuronId destNeuronId newWeight)
  918. X    - Change the connection strength between two neurons
  919. X      or an input and a neuron.  I put this in so that if you
  920. X      wanted to construct a backpropagation network you at least
  921. X      had the means to train it.
  922. X
  923. X(GetWeight srcNeuronId destNeuronId)
  924. X    - Get the connection strength between srcNeuronId and destNeuronId.
  925. X
  926. X(GetSum neuronId)
  927. X    - Get the internal SUM (or NET) of a particular neuron.
  928. X
  929. X*************************************************************************
  930. XRedistribute this program at will.  If you decide to use this program
  931. Xas part of some commercial package you are developing, by all means
  932. Xproceed, all I ask is that you give me a small amount of credit for it.
  933. X
  934. XSend comments, ideas, questions, improvements. donation$ to:
  935. XJason Leigh
  936. X538 Mackenzie Hall, Wayne State University, Detroit, MI 48202
  937. Xe-mail: jal@zeus.cs.wayne.edu
  938. SHAR_EOF
  939. echo "extracting xor.lsp"
  940. sed 's/^X//' << \SHAR_EOF > xor.lsp
  941. X;;; @@FILE and.lsp
  942. X;;; Copyright (C) 1989 By Jason Leigh 5/5/89
  943. X
  944. X;;; Redistribute this program at will.  If you decide to use this program
  945. X;;; as part of some commercial package you are developing, by all means
  946. X;;; proceed, all I ask is that you give me a small amount of credit for it.
  947. X
  948. X;;; XOR created with 2 layer MADALINES.  This uses the most basic
  949. X;;; AND and OR ADALINES and connects them up into the form of
  950. X;;; the XOR expressions which is: ((not a) and (b)) or ((a) and (not b))
  951. X
  952. X(ClearNetwork)
  953. X
  954. X;;; Select the Relay function as the output function.
  955. X(defun OutputFunction (sum threshold)
  956. X    (Relay sum threshold))
  957. X
  958. X(CreateNeuron 'or1 0)        ; Create a neuron for OR
  959. X(CreateInputLine 'i1)        ; Create input line i1
  960. X(CreateInputLine 'i2)        ; Create input line i2
  961. X(CreateInputLine 'unity)    ; Create input line `unity' which is always 1
  962. X(SetInputLine 'unity 1)
  963. X(CreateNeuron 'and1 0)        ; Create two and neurons.
  964. X(CreateNeuron 'and2 0)
  965. X
  966. X(ConnectInputToNeuron 'i1 'and1 1)     ; Make appropriate connects.
  967. X(ConnectInputToNeuron 'i2 'and1 -1)     ; The best thing to do is to
  968. X(ConnectInputToNeuron 'unity 'and1 -1.5) ; get a piece of paper out
  969. X(ConnectInputToNeuron 'i1 'and2 -1)     ; and draw out the connections
  970. X(ConnectInputToNeuron 'i2 'and2 1)     ; to see what the network
  971. X(ConnectInputToNeuron 'unity 'and2 -1.5) ; actually looks like.
  972. X
  973. X(ConnectNeuron 'and1 'or1 1)
  974. X(ConnectNeuron 'and2 'or1 1)
  975. X(ConnectNeuron 'unity 'or1 1.5)
  976. X
  977. X
  978. X;;; Let's try out the network with the 4 possible binary combinations.
  979. X
  980. X(printf "Input: -1 -1\n")
  981. X(SetInputLine 'i1 -1)
  982. X(SetInputLine 'i2 -1)
  983. X(UpdateNetwork 10 '(or1))
  984. X(printf "Output: " (NeuronOutput 'or1) "\n")
  985. X
  986. X(printf "Input: 1 -1\n")
  987. X(SetInputLine 'i1 1)
  988. X(SetInputLine 'i2 -1)
  989. X(UpdateNetwork 10 '(or1))
  990. X(printf "Output: " (NeuronOutput 'or1) "\n")
  991. X
  992. X(printf "Input: -1 1\n")
  993. X(SetInputLine 'i1 -1)
  994. X(SetInputLine 'i2 1)
  995. X(UpdateNetwork 10 '(or1))
  996. X(printf "Output: " (NeuronOutput 'or1) "\n")
  997. X
  998. X(printf "Input: 1 1\n")
  999. X(SetInputLine 'i1 1)
  1000. X(SetInputLine 'i2 1)
  1001. X(UpdateNetwork 10 '(or1))
  1002. X(printf "Output: " (NeuronOutput 'or1) "\n")
  1003. SHAR_EOF
  1004. echo "extracting xor2.lsp"
  1005. sed 's/^X//' << \SHAR_EOF > xor2.lsp
  1006. X;;; @@FILE xor2.lsp
  1007. X;;; Copyright (C) 1989 By Jason Leigh 5/5/89
  1008. X
  1009. X;;; Redistribute this program at will.  If you decide to use this program
  1010. X;;; as part of some commercial package you are developing, by all means
  1011. X;;; proceed, all I ask is that you give me a small amount of credit for it.
  1012. X
  1013. X;;; Creating an XOR network using the functions in neural.lsp
  1014. X
  1015. X;;; The activation function is Sigmoid and a diagram of this
  1016. X;;; network can be found in:
  1017. X;;; Parallel Distributed Processing Volume I: Foundations
  1018. X;;; By David E. Rumelhart, James L. McClelland
  1019. X;;; p.331.
  1020. X
  1021. X(ClearNetwork)
  1022. X
  1023. X;;; Select the Sigmoid function as the output function.
  1024. X(defun OutputFunction (sum threshold)
  1025. X    (Sigmoid sum threshold))
  1026. X
  1027. X(CreateNeuron 'out 6.3)
  1028. X(CreateNeuron 'mid 2.2)
  1029. X(CreateInputLine 'in1)
  1030. X(CreateInputLine 'in2)
  1031. X(ConnectInputToNeuron 'in1 'mid -6.4)
  1032. X(ConnectInputToNeuron 'in1 'out -4.2)
  1033. X(ConnectInputToNeuron 'in2 'mid -6.4)
  1034. X(ConnectInputToNeuron 'in2 'out -4.2)
  1035. X(ConnectNeuron 'mid 'out -9.4)
  1036. X
  1037. X(printf "Input: 0 0\n")
  1038. X(SetInputLine 'in1 0)
  1039. X(SetInputLine 'in2 0)
  1040. X(UpdateNetwork 10 '(mid out))
  1041. X(printf "Output: " (NeuronOutput 'out) "\n")
  1042. X
  1043. X(printf "Input: 1 0\n")
  1044. X(SetInputLine 'in1 1)
  1045. X(SetInputLine 'in2 0)
  1046. X(UpdateNetwork 10 '(mid out))
  1047. X(printf "Output: " (NeuronOutput 'out) "\n")
  1048. X
  1049. X(printf "Input: 0 1\n")
  1050. X(SetInputLine 'in1 0)
  1051. X(SetInputLine 'in2 1)
  1052. X(UpdateNetwork 10 '(mid out))
  1053. X(printf "Output: " (NeuronOutput 'out) "\n")
  1054. X
  1055. X(printf "Input: 1 1\n")
  1056. X(SetInputLine 'in1 1)
  1057. X(SetInputLine 'in2 1)
  1058. X(UpdateNetwork 10 '(mid out))
  1059. X(printf "Output: " (NeuronOutput 'out) "\n")
  1060. SHAR_EOF
  1061. echo "End of archive 1 (of 1)"
  1062. # if you want to concatenate archives, remove anything after this line
  1063. exit
  1064.